<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ext_tender extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		// Uses Model
		$this->load->model('ext_tender_model', 'Tender');
		//$this->load->model('ext_tender_penawaran', 'TenderPenawaran');		
		$this->load->model('combo_model', 'Combo');
		
		// Secure Page With Session
		$this->secure_session();
	}
	
	function index(){
		// Display Index
		$data['title'] = $this->session_menu_label;		
		viewByTemplate('ext_tender/index', $data, 'grid');
	}
	
	function getdata()
	{
		// Ajax Response For Datagrid
		echo $this->Tender->GetJSON();
	}
	
	function getdata_detail()
	{
		// Ajax Response For Datagrid
		echo $this->TenderDetail->GetJSON(post('kode_tender'));
	}	
	
	function create()
	{
		// Display Form Create
		$data['title'] = "Tambah Data Tender";
		$data['data'] = NULL;		
		// Define Combo Data
		$data['arr_application']	= $this->Combo->ITApplication();		
		$data['mode'] = "create";
		viewByTemplate('ext_tender/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Tender";
		$data['data'] = $this->Tender->GetSingleView(get('kode_tender'));
		// Define Combo Data
		$data['arr_application']	= $this->Combo->ITApplication();
		
		$data['mode'] = "edit";
		viewByTemplate('ext_tender/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Tender";
		$data['data'] = $this->Tender->GetSingleView(get('kode_tender'));
		
		// Define Combo Data
		
		$data['arr_application']	= $this->Combo->ITApplication();
		$data['arr_vendor']	= $this->Combo->EXTVendor();
				
		$data['mode'] = "view";
		viewByTemplate('ext_tender/form', $data, 'form');
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty		= $this->MessageTemplate("ER001");
		$tpl_duplicate	= $this->MessageTemplate("ER002");


		// Validation
		if (post("kode_tender") == '') {
			$error->Add("kode_tender", "Kode Tender", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM ext_tender WHERE UPPER(kode_tender) = '".@strtoupper(@trim(post("kode_tender")))."'")) > 0) {
			$error->Add("kode_tender", "Kode Tender", $tpl_duplicate);
		}
		
		return $error;
	}
	
	function ajax_post()
	{
		// Ajax Response For Submit Form
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";
		
		// Get Error Template
		$tpl_insert_success = $this->MessageTemplate("CS001");
		$tpl_update_success = $this->MessageTemplate("CS002");
		
		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data Array
				$data	= post_by_key(array('kode_tender','nama_tender','tgl','app_id','kode_penawaran','keterangan'));
				
				// Set Primary Key Variable
				$data['kode_tender']	= @strtoupper(@trim(post("kode_tender")));
				$kode_tender	= $data['kode_tender'];
				
				// Set Date Format
				// $data['tgl']	= ($data['tgl'] == '' ? NULL : date_format3($data['tgl'], 'YYYY-MM-DD'));				
				
				if (post("mode") == "create")
				{
					$ls_genid	= $kode_tender;
					
					//$data['create_date']	= NOW;
					//$data['create_id']		= USERID;
					
					$this->Tender->Add($data);
					
					$ls_response = $tpl_insert_success['message_id'];
				}
				else if (post("mode") == "edit")
				{
					unset($data['kode_tender']);
					
					//$data['modify_date']	= NOW;
					//$data['modify_id']		= USERID;
					
					$this->Tender->Update(	$data, 
											array('kode_tender'	=> $kode_tender));
					
					$ls_response = $tpl_update_success['message_id'];
				}												
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}
	
	
	function ajax_delete()
	{
		$ls_result	= "";
		$kode_tender	= get('kode_tender');
		
		if ($kode_tender != "")
		{
			// Delete Single
			$arr_rule	= array('kode_tender' => $kode_tender);
			$ls_result = $this->Master->DeleteSingle($arr_rule);
		}
		else
		{
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_tender' => $row['kode_tender']);
				$ls_result .= $this->Tender->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}

	/* ----- >> Example For Print Using FPDF
	 Publisher	: www.fpdf.org
	 Usage		: just copy all function, set variable for data, set view for layout
	 Log		:	Date			PIC			Desc
					18/02/2013		Pitra		Add New Example
	*/
	function cetak(){
		$as_kode_tender	= get('kode_tender');
		
		if($as_kode_tender!='' && $as_kode_tender!=''){
			
			// Load FPDF
			$this->load->helper('fpdf');
			
			// Set Data
			$data['data']			= $this->Tender->GetSingleView($as_kode_tender);
			
			// Load View
			$this->load->view('ext_tender/cetak', $data);
			
		}
	}	

}
?>